@page "/fss/tasks/finishlist/{pageIndex:int}"
@page "/fss/tasks/finishlist"
@using FOPS.Abstract.Fss.Server
@using FOPS.Abstract.Fss.Entity
@using FOPS.Abstract.Fss.Enum
@using FS.Extends
@using System.Timers
@inject IIocManager _iocManager;
@inject NavigationManager nav;
@inject IJSRuntime js;
@inject Blazored.LocalStorage.ILocalStorageService storage;
@implements IDisposable;

<div class="layui-card">
    <div class="layui-card-header" id="anchor-play-header">
        完成的任务
        <div class="layui-right">
            <button class="layui-btn layui-btn-right" @onclick=GotoList><i class="am-icon-plus"></i>任务组列表</button>
        </div>
    </div>
    <div class="layui-card-body">
        <div class="layui-form layui-border-box layui-table-view" lay-filter="LAY-table-1" style=" ">
            <div class="layui-table-box">
                <div class="layui-table-header">
                    <table cellspacing="0" cellpadding="0" border="0" class="layui-table" lay-skin="auto" lay-size="sm">
                        <thead>
                        <tr>
                            <th data-field="Type">
                                <div class="layui-table-cell laytable-cell-task-1">
                                    <span>任务名称</span>
                                </div>
                            </th>
                            <th data-field="Type">
                                <div class="layui-table-cell laytable-cell-task-2">
                                    <span>计划时间</span>
                                </div>
                            </th>
                            <th data-field="Type">
                                <div class="layui-table-cell laytable-cell-task-3">
                                    <span>实际运行</span>
                                </div>
                            </th>
                            <th data-field="Type">
                                <div class="layui-table-cell laytable-cell-task-4">
                                    <span>耗时</span>
                                </div>
                            </th>
                            <th data-field="Type">
                                <div class="layui-table-cell laytable-cell-task-5">
                                    <span>进度</span>
                                </div>
                            </th>
                            <th data-field="Type">
                                <div class="layui-table-cell laytable-cell-task-6">
                                    <span>状态</span>
                                </div>
                            </th>
                            <th data-field="Type">
                                <div class="layui-table-cell laytable-cell-task-7">
                                    <span>客户端IP</span>
                                </div>
                            </th>
                            <th data-field="Type">
                                <div class="layui-table-cell laytable-cell-task-8">
                                    <span>创建时间</span>
                                </div>
                            </th>
                        </tr>
                        </thead>
                    </table>
                </div>
                <div class="layui-table-body layui-table-main">
                    <table cellspacing="0" cellpadding="0" border="0" class="layui-table" lay-skin="auto" lay-size="sm">
                        <tbody>
                        @for (var index = 0; index < _lst.Count; index++)
                        {
                            var info = _lst[index];
                            <tr data-index="@index">
                                <td>
                                    <div class="laytable-cell-task-1">【@info.JobName】@info.Caption</div>
                                </td>
                                <td>
                                    <div class="layui-table-cell laytable-cell-task-2">@info.StartAt.ToString("yyyy-MM-dd HH:mm:ss")</div>
                                </td>
                                <td>
                                    <div class="layui-table-cell laytable-cell-task-3">
                                        @if (info.Status is EumTaskType.Working or EumTaskType.Fail or EumTaskType.Success)
                                        {
                                            @info.RunAt.ToString("yyyy-MM-dd HH:mm:ss");
                                        }
                                    </div>
                                </td>
                                <td>
                                    <div class="layui-table-cell laytable-cell-task-4">@info.RunSpeed ms</div>
                                </td>
                                <td>
                                    <div class="layui-table-cell laytable-cell-task-5">@info.Progress%</div>
                                </td>
                                <td>
                                    <div class="layui-table-cell laytable-cell-task-6">
                                        @switch (info.Status)
                                        {
                                            case EumTaskType.None:
                                                <button class="layui-btn layui-btn-xs layui-btn-primary">@info.Status.GetName()</button>
                                                break;
                                            case EumTaskType.Scheduler:
                                                <button class="layui-btn layui-btn-xs layui-btn-normal">@info.Status.GetName()</button>
                                                break;
                                            case EumTaskType.Working:
                                                <button class="layui-btn layui-btn-xs layui-btn-normal">@info.Status.GetName()</button>
                                                break;
                                            case EumTaskType.Fail:
                                                <button class="layui-btn layui-btn-xs layui-btn-danger">@info.Status.GetName()</button>
                                                break;
                                            case EumTaskType.Success:
                                                <button class="layui-btn layui-btn-xs">@info.Status.GetName()</button>
                                                break;
                                            case EumTaskType.ReScheduler:
                                                <button class="layui-btn layui-btn-xs layui-btn-danger">@info.Status.GetName()</button>
                                                break;
                                        }
                                    </div>
                                </td>
                                <td>
                                    <div class="layui-table-cell laytable-cell-task-7">@info.ClientIp</div>
                                </td>
                                <td>
                                    <div class="layui-table-cell laytable-cell-task-8">@info.CreateAt.ToString("yyyy-MM-dd HH:mm:ss")</div>
                                </td>
                            </tr>
                        }
                        </tbody>
                    </table>
                </div>
            </div>
            <PageSplit PageIndex="@PageIndex" PageSize="@_pageSize" TotalCount="@_totalCount" PageUrl="/fss/tasks/finishlist/{index}"></PageSplit>
        </div>
    </div>
</div>
<style>
.laytable-cell-task-1 {width: 300px;}
.laytable-cell-task-2 {width: 150px;}
.laytable-cell-task-3 {width: 150px;}
.laytable-cell-task-4 {width: 100px;}
.laytable-cell-task-5 {width: 100px;}
.laytable-cell-task-6 {width: 80px;}
.laytable-cell-task-7 {width: 120px;}
.laytable-cell-task-8 {width: 150px;}
</style>

@code{

    private int _lastIndex;
    [Parameter] public int PageIndex { get; set; } = 1;
    private long _totalCount;
    private int _pageSize = 20;
    private List<TaskVO> _lst = new();
    private Timer _timer;
    private bool _isFirst;

    protected override async Task OnParametersSetAsync()
    {
        if (_isFirst && _lastIndex == PageIndex) return;
        _isFirst = true;

        if (PageIndex < 1) PageIndex = 1;
        _lastIndex = PageIndex;
        
        var page = await _iocManager.Resolve<IFssApi>().GetTaskFinishListAsync(storage,_pageSize, PageIndex);
        _lst = page?.List;
        _totalCount = page.RecordCount;
    }

    protected override Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            _timer = new Timer(1000);
            _timer.Elapsed += async (_, _) =>
            {
                try
                {
                    var page = await _iocManager.Resolve<IFssApi>().GetTaskFinishListAsync(storage,_pageSize, PageIndex);
                    _lst = page?.List;
                    _totalCount = page.RecordCount;
    // 刷新页面
                    await InvokeAsync(StateHasChanged);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            };
            _timer.Enabled = true;
        }
        return Task.FromResult(0);
    }

    private void GotoList()
    {
        nav.NavigateTo("/fss/task_group/list");
    }

    public void Dispose()
    {
        _timer?.Dispose();
    }
}